home *** CD-ROM | disk | FTP | other *** search
/ Aminet 4 / Aminet 4 - November 1994.iso / aminet / util / gnu / gnuplot_3_5.lha / gnuplot / winwprinter.c < prev    next >
C/C++ Source or Header  |  1993-09-16  |  11KB  |  410 lines

  1. #ifndef lint
  2. static char *RCSid = "$Id: wprinter.c%v 3.50.1.13 1993/08/19 03:21:26 woo Exp $";
  3. #endif
  4.  
  5. /* GNUPLOT - win/wprinter.c */
  6. /*
  7.  * Copyright (C) 1992   Maurice Castro, Russell Lang
  8.  *
  9.  * Permission to use, copy, and distribute this software and its
  10.  * documentation for any purpose with or without fee is hereby granted, 
  11.  * provided that the above copyright notice appear in all copies and 
  12.  * that both that copyright notice and this permission notice appear 
  13.  * in supporting documentation.
  14.  *
  15.  * Permission to modify the software is granted, but not the right to
  16.  * distribute the modified code.  Modifications are to be distributed 
  17.  * as patches to released version.
  18.  *  
  19.  * This software is provided "as is" without express or implied warranty.
  20.  * 
  21.  *
  22.  * AUTHORS
  23.  * 
  24.  *   Maurice Castro
  25.  *   Russell Lang
  26.  * 
  27.  * Send your comments or suggestions to 
  28.  *  info-gnuplot@dartmouth.edu.
  29.  * This is a mailing list; to join it send a note to 
  30.  *  info-gnuplot-request@dartmouth.edu.  
  31.  * Send bug reports to
  32.  *  bug-gnuplot@dartmouth.edu.
  33.  */
  34.  
  35. /* Dump a file to the printer */
  36.  
  37. #define STRICT
  38. #include <windows.h>
  39. #include <windowsx.h>
  40. #if WINVER >= 0x030a
  41. #include <commdlg.h>
  42. #endif
  43. #ifdef __MSC__
  44. #include <memory.h>
  45. #else
  46. #include <mem.h>
  47. #endif
  48. #include "wgnuplib.h"
  49. #include "wresourc.h"
  50. #include "wcommon.h"
  51.  
  52. LPPRINT prlist = NULL;
  53.  
  54. BOOL CALLBACK _export PrintSizeDlgProc(HWND hdlg, UINT wmsg, WPARAM wparam, LPARAM lparam);
  55.  
  56. BOOL CALLBACK _export
  57. PrintSizeDlgProc(HWND hdlg, UINT wmsg, WPARAM wparam, LPARAM lparam)
  58. {
  59.     char buf[8];
  60.     LPPRINT lpr;
  61.     lpr = (LPPRINT)GetWindowLong(GetParent(hdlg), 4);
  62.  
  63.     switch (wmsg) {
  64.         case WM_INITDIALOG:
  65.             wsprintf(buf,"%d",lpr->pdef.x);
  66.             SetDlgItemText(hdlg, PSIZE_DEFX, buf);
  67.             wsprintf(buf,"%d",lpr->pdef.y);
  68.             SetDlgItemText(hdlg, PSIZE_DEFY, buf);
  69.             wsprintf(buf,"%d",lpr->poff.x);
  70.             SetDlgItemText(hdlg, PSIZE_OFFX, buf);
  71.             wsprintf(buf,"%d",lpr->poff.y);
  72.             SetDlgItemText(hdlg, PSIZE_OFFY, buf);
  73.             wsprintf(buf,"%d",lpr->psize.x);
  74.             SetDlgItemText(hdlg, PSIZE_X, buf);
  75.             wsprintf(buf,"%d",lpr->psize.y);
  76.             SetDlgItemText(hdlg, PSIZE_Y, buf);
  77.             CheckDlgButton(hdlg, PSIZE_DEF, TRUE);
  78.             EnableWindow(GetDlgItem(hdlg, PSIZE_X), FALSE);
  79.             EnableWindow(GetDlgItem(hdlg, PSIZE_Y), FALSE);
  80.             return TRUE;
  81.         case WM_COMMAND:
  82.             switch (wparam) {
  83.                 case PSIZE_DEF:
  84.                     EnableWindow(GetDlgItem(hdlg, PSIZE_X), FALSE);
  85.                     EnableWindow(GetDlgItem(hdlg, PSIZE_Y), FALSE);
  86.                     return FALSE;
  87.                 case PSIZE_OTHER:
  88.                     EnableWindow(GetDlgItem(hdlg, PSIZE_X), TRUE);
  89.                     EnableWindow(GetDlgItem(hdlg, PSIZE_Y), TRUE);
  90.                     return FALSE;
  91.                 case IDOK:
  92.                     if (SendDlgItemMessage(hdlg, PSIZE_OTHER, BM_GETCHECK, 0, 0L)) {
  93.                         SendDlgItemMessage(hdlg, PSIZE_X, WM_GETTEXT, 7, (LPARAM)((LPSTR)buf));
  94.                         GetInt(buf, &lpr->psize.x);
  95.                         SendDlgItemMessage(hdlg, PSIZE_Y, WM_GETTEXT, 7, (LPARAM)((LPSTR)buf));
  96.                         GetInt(buf, &lpr->psize.y);
  97.                     }
  98.                     else {
  99.                         lpr->psize.x = lpr->pdef.x;
  100.                         lpr->psize.y = lpr->pdef.y;
  101.                     }
  102.                     SendDlgItemMessage(hdlg, PSIZE_OFFX, WM_GETTEXT, 7, (LPARAM)((LPSTR)buf));
  103.                     GetInt(buf, &lpr->poff.x);
  104.                     SendDlgItemMessage(hdlg, PSIZE_OFFY, WM_GETTEXT, 7, (LPARAM)((LPSTR)buf));
  105.                     GetInt(buf, &lpr->poff.y);
  106.  
  107.                     if (lpr->psize.x <= 0)
  108.                         lpr->psize.x = lpr->pdef.x;
  109.                     if (lpr->psize.y <= 0)
  110.                         lpr->psize.y = lpr->pdef.y;
  111.  
  112.                     EndDialog(hdlg, IDOK);
  113.                     return TRUE;
  114.                 case IDCANCEL:
  115.                     EndDialog(hdlg, IDCANCEL);
  116.                     return TRUE;
  117.             }
  118.             break;
  119.     }
  120.     return FALSE;
  121. }
  122.  
  123.  
  124.  
  125. /* GetWindowLong(hwnd, 4) must be available for use */
  126. BOOL
  127. PrintSize(HDC printer, HWND hwnd, LPRECT lprect)
  128. {
  129. HDC hdc;
  130. DLGPROC lpfnPrintSizeDlgProc ;
  131. BOOL status = FALSE;
  132. PRINT pr;
  133.  
  134.     SetWindowLong(hwnd, 4, (LONG)((LPPRINT)&pr));
  135.     pr.poff.x = 0;
  136.     pr.poff.y = 0;
  137.     pr.psize.x = GetDeviceCaps(printer, HORZSIZE);
  138.     pr.psize.y = GetDeviceCaps(printer, VERTSIZE);
  139.     hdc = GetDC(hwnd);
  140.     GetClientRect(hwnd,lprect);
  141.     pr.pdef.x = MulDiv(lprect->right-lprect->left, 254, 10*GetDeviceCaps(hdc, LOGPIXELSX));
  142.     pr.pdef.y = MulDiv(lprect->bottom-lprect->top, 254, 10*GetDeviceCaps(hdc, LOGPIXELSX));
  143.     ReleaseDC(hwnd,hdc);
  144. #ifdef __DLL__
  145.     lpfnPrintSizeDlgProc = (DLGPROC)GetProcAddress(hdllInstance, "PrintSizeDlgProc");
  146. #else
  147.     lpfnPrintSizeDlgProc = (DLGPROC)MakeProcInstance((FARPROC)PrintSizeDlgProc, hdllInstance);
  148. #endif
  149.     if (DialogBox (hdllInstance, "PrintSizeDlgBox", hwnd, lpfnPrintSizeDlgProc)
  150.         == IDOK) {
  151.         lprect->left = MulDiv(pr.poff.x*10, GetDeviceCaps(printer, LOGPIXELSX), 254);
  152.         lprect->top = MulDiv(pr.poff.y*10, GetDeviceCaps(printer, LOGPIXELSY), 254);
  153.         lprect->right = lprect->left + MulDiv(pr.psize.x*10, GetDeviceCaps(printer, LOGPIXELSX), 254);
  154.         lprect->bottom = lprect->top + MulDiv(pr.psize.y*10, GetDeviceCaps(printer, LOGPIXELSY), 254);
  155.         status = TRUE;
  156.     }
  157. #ifndef __DLL__
  158.     FreeProcInstance((FARPROC)lpfnPrintSizeDlgProc);
  159. #endif
  160.     SetWindowLong(hwnd, 4, (LONG)(0L));
  161.     return status;
  162. }
  163.  
  164. void 
  165. PrintRegister(LPPRINT lpr)
  166. {
  167.     LPPRINT next;
  168.     next = prlist;
  169.     prlist = lpr;
  170.     lpr->next = next;
  171. }
  172.  
  173. LPPRINT
  174. PrintFind(HDC hdc)
  175. {
  176.     LPPRINT this;
  177.     this = prlist;
  178.     while (this && (this->hdcPrn!=hdc)) {
  179.         this = this->next;
  180.     }
  181.     return this;
  182. }
  183.  
  184. void
  185. PrintUnregister(LPPRINT lpr)
  186. {
  187.     LPPRINT this, prev;
  188.     prev = (LPPRINT)NULL;
  189.     this = prlist;
  190.     while (this && (this!=lpr)) {
  191.         prev = this;
  192.         this = this->next;
  193.     }
  194.     if (this && (this == lpr)) {
  195.         /* unhook it */
  196.         if (prev)
  197.             prev->next = this->next;
  198.         else
  199.             prlist = this->next;
  200.     }
  201. }
  202.  
  203.  
  204. /* GetWindowLong(GetParent(hDlg), 4) must be available for use */
  205. BOOL CALLBACK _export
  206. PrintDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  207. {
  208.     LPPRINT lpr;
  209.     lpr = (LPPRINT)GetWindowLong(GetParent(hDlg), 4);
  210.  
  211.     switch(message) {
  212.         case WM_INITDIALOG:
  213.             lpr->hDlgPrint = hDlg;
  214.             SetWindowText(hDlg,(LPSTR)lParam);
  215.             EnableMenuItem(GetSystemMenu(hDlg,FALSE),SC_CLOSE,MF_GRAYED);
  216.             return TRUE;
  217.         case WM_COMMAND:
  218.             lpr->bUserAbort = TRUE;
  219.             lpr->hDlgPrint = 0;
  220.             EnableWindow(GetParent(hDlg),TRUE);
  221.             EndDialog(hDlg, FALSE);
  222.             return TRUE;
  223.     }
  224.     return FALSE;
  225. }
  226.  
  227.     
  228. BOOL CALLBACK _export
  229. PrintAbortProc(HDC hdcPrn, int code)
  230. {
  231.     MSG msg;
  232.     LPPRINT lpr;
  233.     lpr = PrintFind(hdcPrn);
  234.  
  235.     while (!lpr->bUserAbort && PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
  236.         if (!lpr->hDlgPrint || !IsDialogMessage(lpr->hDlgPrint,&msg)) {
  237.             TranslateMessage(&msg);
  238.             DispatchMessage(&msg);
  239.         }
  240.     }
  241.     return(!lpr->bUserAbort);
  242. }
  243.  
  244.  
  245.  
  246. /* documented in Device Driver Adaptation Guide */
  247. /* Prototypes taken from print.h */
  248. DECLARE_HANDLE(HPJOB);
  249.  
  250. HPJOB   WINAPI OpenJob(LPSTR, LPSTR, HPJOB);
  251. int     WINAPI StartSpoolPage(HPJOB);
  252. int     WINAPI EndSpoolPage(HPJOB);
  253. int     WINAPI WriteSpool(HPJOB, LPSTR, int);
  254. int     WINAPI CloseJob(HPJOB);
  255. int     WINAPI DeleteJob(HPJOB, int);
  256. int     WINAPI WriteDialog(HPJOB, LPSTR, int);
  257. int     WINAPI DeleteSpoolPage(HPJOB);
  258.  
  259.  
  260. /* Modeless dialog box - Cancel printing */
  261. BOOL CALLBACK _export
  262. CancelDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  263. {
  264.     switch(message) {
  265.     case WM_INITDIALOG:
  266.         SetWindowText(hDlg,(LPSTR)lParam);
  267.         return TRUE;
  268.     case WM_COMMAND:
  269.         switch(LOWORD(wParam)) {
  270.         case IDCANCEL:
  271.             DestroyWindow(hDlg);
  272.             return TRUE;
  273.         }
  274.     }
  275.     return FALSE;
  276. }
  277.  
  278. /* Dialog box to select printer port */
  279. BOOL CALLBACK _export
  280. SpoolDlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
  281. {
  282. LPSTR entry;
  283.     switch(message) {
  284.     case WM_INITDIALOG:
  285.         entry = (LPSTR)lParam;
  286.         while (*entry) {
  287.         SendDlgItemMessage(hDlg, SPOOL_PORT, LB_ADDSTRING, 0, (LPARAM)entry);
  288.         entry += lstrlen(entry)+1;
  289.         }
  290.         SendDlgItemMessage(hDlg, SPOOL_PORT, LB_SETCURSEL, 0, (LPARAM)0);
  291.         return TRUE;
  292.     case WM_COMMAND:
  293.         switch(LOWORD(wParam)) {
  294.         case SPOOL_PORT:
  295. #ifdef WIN32
  296.             if (HIWORD(wParam)
  297. #else
  298.             if (HIWORD(lParam)
  299. #endif
  300.                            == LBN_DBLCLK)
  301.             PostMessage(hDlg, WM_COMMAND, IDOK, 0L);
  302.             return FALSE;
  303.         case IDOK:
  304.             EndDialog(hDlg, 1+(int)SendDlgItemMessage(hDlg, SPOOL_PORT, LB_GETCURSEL, 0, 0L));
  305.             return TRUE;
  306.         case IDCANCEL:
  307.             EndDialog(hDlg, 0);
  308.             return TRUE;
  309.         }
  310.     }
  311.     return FALSE;
  312. }
  313.  
  314. /* Print File to port */
  315. void WDPROC
  316. DumpPrinter(HWND hwnd, LPSTR szAppName, LPSTR szFileName)
  317. {
  318. #define PRINT_BUF_SIZE 4096
  319. char *buffer;
  320. char *portname;
  321. DLGPROC lpfnSpoolProc;
  322. int i, iport;
  323. HPJOB hJob;
  324. UINT count;
  325. HFILE hf;
  326. int error = FALSE;
  327. DLGPROC lpfnCancelProc;
  328. long lsize;
  329. long ldone;
  330. char fmt[64];
  331. char pcdone[10];
  332. MSG msg;
  333. HWND hDlgModeless;
  334.  
  335.     if ((buffer = LocalAllocPtr(LHND, PRINT_BUF_SIZE)) == (char *)NULL)
  336.         return;
  337.     /* get list of ports */
  338.     GetProfileString("ports", NULL, "", buffer, PRINT_BUF_SIZE);
  339.     /* select a port */
  340.     lpfnSpoolProc = (DLGPROC)MakeProcInstance((FARPROC)SpoolDlgProc, hdllInstance);
  341.     iport = DialogBoxParam(hdllInstance, "SpoolDlgBox", hwnd, lpfnSpoolProc, (LPARAM)buffer);
  342.     FreeProcInstance((FARPROC)lpfnSpoolProc);
  343.     if (!iport) {
  344.         LocalFreePtr((void NEAR *)buffer);
  345.         return;
  346.     }
  347.     portname = buffer;
  348.     for (i=1; i<iport && lstrlen(portname)!=0; i++)
  349.         portname += lstrlen(portname)+1;
  350.     
  351.     /* open file and get length */
  352.     hf = _lopen(szFileName, READ);
  353.     if (hf == HFILE_ERROR) {
  354.         LocalFreePtr((void NEAR *)buffer);
  355.         return;
  356.     }
  357.     lsize = _llseek(hf, 0L, 2);
  358.     (void)_llseek(hf, 0L, 0);
  359.     if (lsize <= 0)
  360.         lsize = 1;
  361.  
  362.     hJob = OpenJob(portname, szFileName, (HDC)NULL);
  363.     switch ((int)hJob) {
  364.         case SP_APPABORT:
  365.         case SP_ERROR:
  366.         case SP_OUTOFDISK:
  367.         case SP_OUTOFMEMORY:
  368.         case SP_USERABORT:
  369.             _lclose(hf);
  370.         LocalFreePtr((void NEAR *)buffer);
  371.             return;
  372.     }
  373.     if (StartSpoolPage(hJob) < 0)
  374.         error = TRUE;
  375.  
  376.     ldone = 0;
  377.     lpfnCancelProc = (DLGPROC)MakeProcInstance((FARPROC)CancelDlgProc, hdllInstance);
  378.     hDlgModeless = CreateDialogParam(hdllInstance, "CancelDlgBox", hwnd, lpfnCancelProc, (LPARAM)szAppName);
  379.  
  380.     while (!error && hDlgModeless && IsWindow(hDlgModeless)
  381.           && ((count = _lread(hf, buffer, PRINT_BUF_SIZE))!= 0) ) {
  382.         wsprintf(pcdone, "%d%% done", (int)(ldone * 100 / lsize));
  383.         SetWindowText(GetDlgItem(hDlgModeless, CANCEL_PCDONE), pcdone);
  384.         if (WriteSpool(hJob, buffer, count) < 0)
  385.         error = TRUE;
  386.         ldone += count;
  387.         while (IsWindow(hDlgModeless) && PeekMessage(&msg, hDlgModeless, 0, 0, PM_REMOVE)) {
  388.             if (!IsDialogMessage(hDlgModeless, &msg)) {
  389.             TranslateMessage(&msg);
  390.             DispatchMessage(&msg);
  391.         }
  392.         }
  393.     }
  394.     LocalFreePtr((void NEAR *)buffer);
  395.     _lclose(hf);
  396.  
  397.     if (!hDlgModeless || !IsWindow(hDlgModeless))
  398.         error=TRUE;
  399.     if (IsWindow(hDlgModeless))
  400.         DestroyWindow(hDlgModeless);
  401.     hDlgModeless = 0;
  402.     FreeProcInstance((FARPROC)lpfnCancelProc);
  403.     EndSpoolPage(hJob);
  404.     if (error)
  405.         DeleteJob(hJob, 0);
  406.     else
  407.         CloseJob(hJob);
  408. }
  409.  
  410.